package com.drug.mingkang.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.drug.mingkang.mapper.*;
import com.drug.mingkang.model.*;
import com.drug.mingkang.service.OrderReturnApplyService;
import com.drug.mingkang.utils.Tools;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 退货订单表(OrderReturnApply)表服务实现类
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:14
 */
@Service("orderReturnApplyService")
public class OrderReturnApplyServiceImpl implements OrderReturnApplyService {
    @Resource
    private OrderReturnApplyMapper orderReturnApplyMapper;
    @Resource
    private ImmPurchaseDeatisMapper immPurchaseDeatisMapper;
    @Resource
    private ImmPurchaseMapper immPurchaseMapper;
    @Resource
    private ReturnDetailMapper returnDetailMapper;
    @Resource
    private HqReviewMapper hqReviewMapper;
    @Resource
    private Tools tools;
    @Resource
    private HqMarketMapper hqMarketMapper;


    /**
     * 雪花id
     */
    private long workerId=0L;
    private long datacenterId=1L;
    private Snowflake snowflake= IdUtil.createSnowflake(workerId, datacenterId);
    /**
     * 通过ID查询单条数据
     *
     * @param orderId 主键
     * @return 实例对象
     */
    @Override
    public OrderReturnApply queryById(String orderId) {
        return this.orderReturnApplyMapper.queryById(orderId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<OrderReturnApply> queryAllByLimit(int offset, int limit) {
        return this.orderReturnApplyMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param orderReturnApply 实例对象
     * @return 实例对象
     */
    @Override
    public OrderReturnApply insert(OrderReturnApply orderReturnApply) {
        this.orderReturnApplyMapper.insert(orderReturnApply);
        return orderReturnApply;
    }

    /**
     * 修改数据
     *
     * @param orderReturnApply 实例对象
     * @return 实例对象
     */
    @Override
    public OrderReturnApply update(OrderReturnApply orderReturnApply) {
        this.orderReturnApplyMapper.update(orderReturnApply);
        return this.queryById(orderReturnApply.getOrderId());
    }

    /**
     * 通过主键删除数据
     *
     * @param orderId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String orderId) {
        return this.orderReturnApplyMapper.deleteById(orderId) > 0;
    }

    @Override
    public List<ImmPurchaseDeatis> listPurchase(String purchaseId) {
        List<ImmPurchaseDeatis> immPurchaseDeatis = immPurchaseDeatisMapper.selectPurchaseId(purchaseId);
        return immPurchaseDeatis;
    }


    /**
     * 递归
     * @param drugTypeId 药品类型id
     * @return
     */
    @Override
    public List<HqDrug> selectDigui(String drugTypeId) {
        return null;
    }

    /**
     * 退款申请
     * @param jsonmes
     * @param refundCause
     */
    @Override
    public String refundPutIn(String jsonmes, String refundCause) {
        //药品总数量
        Integer drugCount = 0 ;
        //药品总价格
        Double  commodityPrice = 0.0 ;
        System.out.println("jsonmes"+jsonmes);
        List<HqDrug> hqDrugs = tools.jsonToList(jsonmes, HqDrug.class);
        //设置id的唯一性
        Set set = new HashSet();
        //增加退货订单以及退货详情
        for(int i=0;i<hqDrugs.size();i++){
            //总数量
            drugCount+=hqDrugs.get(i).getCount();
            //总价格
            commodityPrice+=hqDrugs.get(i).getDrugOut()*hqDrugs.get(i).getCount();
            //增加退货id   在增加退货订单之前需要先增加退货详情
            if(set.add(hqDrugs.get(i).getPurchaseId())){
                OrderReturnApply orderReturnApply = new OrderReturnApply();
                orderReturnApply.setOrderId(hqDrugs.get(i).getPurchaseId());
                orderReturnApplyMapper.insertTwo(orderReturnApply);
            }
            ReturnDetail returnDetail = new ReturnDetail();
            //退货详情明细
            returnDetail.setDetailId(hqDrugs.get(i).getPurchaseId());
            returnDetail.setCounts(hqDrugs.get(i).getCount());
            returnDetail.setMoney(hqDrugs.get(i).getDrugOut());
            returnDetail.setDrug(hqDrugs.get(i).getDrugId());
            returnDetail.setIsva(1);
            //订单id
            returnDetail.setOrderId(hqDrugs.get(i).getPurchaseId());
            //操作人                                                               操作人从session中获取
            returnDetail.setOper("1");
            returnDetailMapper.insert(returnDetail);
            //增加退货订单
            if(i==hqDrugs.size()-1){
                //退款订单
                OrderReturnApply orderReturnApply = new OrderReturnApply();
                //退货订单id
                orderReturnApply.setOrderId(hqDrugs.get(i).getPurchaseId());
                //退货总量
                orderReturnApply.setProductCount(drugCount);

                //退户原因
                orderReturnApply.setReason(refundCause);
                //是否有效                                                                  1 有效 2 无效
                orderReturnApply.setIsva(1);
                //分公司id                                                                 需要从session中获取
                orderReturnApply.setImmID(1+"");
                //操作人                                                                   需要从session中获取
                orderReturnApply.setOper(1+"");
                //销售订单id
                orderReturnApply.setPurchaseId(hqDrugs.get(i).getPurchaseId());
                //总价格
                orderReturnApply.setProductPrice(commodityPrice);
                //总数量
                orderReturnApply.setProductCount(drugCount);
                //订单状态
                orderReturnApply.setApplyState(65);
                //修改
                orderReturnApplyMapper.update(orderReturnApply);
                //修改分公司的订单状态
                ImmPurchase immPurchase = new ImmPurchase();
                immPurchase.setPurchaseId(hqDrugs.get(i).getPurchaseId());
                immPurchase.setIsva(65);
                immPurchaseMapper.update(immPurchase);
                //修改总公司的销售状态
                HqMarket hqMarket = new HqMarket();
                hqMarket.setBranch(hqDrugs.get(i).getPurchaseId());
                hqMarket.setIsva(65);
                hqMarketMapper.update(hqMarket);
            }
        }
        






        //使用第三方表
//        long l = snowflake.nextId();
//        boolean once = true;
//        //将json转化成list
//        List<HqDrug> hqDrugs = JSONObject.parseArray(jsonmes, HqDrug.class);
//        //退款明细
//        int count =0;
//        String purchaseId = null;
//        double money = 0;
//        for(int i=0;i<hqDrugs.size();i++){
//            //判定唯一性
//            if(once){
//                OrderReturnApply orderReturnApply1 = orderReturnApplyMapper.queryPurchaseId(hqDrugs.get(i).getPurchaseId());// 查看数据库中是否已经申请过这个订单的退货
//                //唯一退款
//                if(!StringUtils.isEmpty(orderReturnApply1)){
//                    return "申请失败,一个订单只能退货一次哦!!!" ;
//                }
//                //增加主键
//                OrderReturnApply orderReturnApply2 = new OrderReturnApply();
//                orderReturnApply2.setOrderId(l+"");
//                orderReturnApplyMapper.insertTwo(orderReturnApply2);
//            }
//            once = false;
//            //退款明细
//            //总数量
//            count+=hqDrugs.get(i).getCount();
//            //总价格
//            money+=hqDrugs.get(i).getDrugOut()*hqDrugs.get(i).getCount();
//            //分公司采购订单id
//            purchaseId = hqDrugs.get(i).getPurchaseId();
//            //uuid
//            String uuid = UUID.randomUUID().toString();
//            ReturnDetail returnDetail = new ReturnDetail();
//            //退货详情明细
//            returnDetail.setDetailId(uuid);
//            returnDetail.setCounts(hqDrugs.get(i).getCount());
//            returnDetail.setMoney(hqDrugs.get(i).getDrugOut());
//            returnDetail.setDrug(hqDrugs.get(i).getDrugId());
//            returnDetail.setIsva(1);
//            //订单id
//            returnDetail.setOrderId(l+"");//l+""
//            //操作人                                                               操作人从session中获取
//            returnDetail.setOper("1");
//            //增加方法
//            returnDetailMapper.insert(returnDetail);
//            //最后一次
//            if(i==hqDrugs.size()-1){
//                //退款订单
//                OrderReturnApply orderReturnApply = new OrderReturnApply();
//                //退货订单id
//                orderReturnApply.setOrderId(l+"");
//                //退货申请
//                orderReturnApply.setApplyState(80);
//                //退货总量
//                orderReturnApply.setProductCount(count);
//
//                //退户原因
//                orderReturnApply.setReason(refundCause);
//                //是否有效                                                                  1 有效 2 无效
//                orderReturnApply.setIsva(1);
//                //分公司id                                                                 需要从session中获取
//                orderReturnApply.setImmID(1+"");
//                //操作人                                                                   需要从session中获取
//                orderReturnApply.setOper(1+"");
//                //销售订单id
//                orderReturnApply.setPurchaseId(purchaseId);
//                //总价格
//                orderReturnApply.setProductPrice(money);
//                //总数量
//                orderReturnApply.setProductCount(count);
//                //增加方法
//                orderReturnApplyMapper.update(orderReturnApply);
//            }
//        }
//        //                                                                          审核表中的数据
//        //                                                                          uuid
//        String uuid2 = UUID.randomUUID().toString();
//        HqReview hqReview = new HqReview();
//        hqReview.setReviewId(uuid2);
//        hqReview.setReviewState("80");
//        hqReview.setReviewRemark("退款退货");
//        hqReview.setOperator("1");//                                                需要从session中获取
//        //业务id
//        hqReview.setBusinessId(l+"");
//        int insert = hqReviewMapper.inserTwo(hqReview);
//        return "申请成功";
        return null;
    }


}